package com.example.demo.utils.ResolveUtils;


import com.itextpdf.text.pdf.PdfReader;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

@Slf4j
public class TxtUtil {

    /**
     * .
     * 获取文本内容
     *
     * @return 文件内容
     */
    public static String readTxtFile(String filePath) {
        String txtStr = "";
        try {
            File file = new File(filePath);
            if (!file.exists()) {
                log.error("可读文件不存在[{}]", file.getAbsolutePath());
            }
            txtStr = FileUtils.readFileToString(file, CharsetUtil.UTF_8);
        } catch (IOException e) {
            log.error("readTxtFile [{}] is error", filePath);
        }
        txtStr = txtStr.replace("\n", "");
        txtStr = txtStr.replace("\\\\r", "");
        txtStr = txtStr.replace("\\\\t", "");
        return txtStr;
    }


    /**
     * .
     * 写入数据
     *
     * @param outPath 输出路径
     * @param context 内容
     */
    public static void writeFile(String outPath, String context) {
        try {
            File file = new File(outPath);
            FileUtils.write(file, context, CharsetUtil.UTF_8, false);
        } catch (IOException e) {
            log.error("writeFile is error {}", e);
        }
    }

    public static String gettxt(String pdfPath) {
        String content = null;
        try {
            // 是否排序
            boolean sort = false;
            // 开始提取页数
            int startPage = 1;
            // 结束提取页数
            int endPage = Integer.MAX_VALUE;
            //InputStream input = null;
            //linux---start---
            // pdfPath = pdfPath.replaceAll("\\\\", File.separator);
            //linux--end----
            File pdfFile = new File(pdfPath);
            PDDocument document = null;

            try (InputStream input = new FileInputStream(pdfFile)) {
                // 加载 pdf 文档
                PDFParser parser = new PDFParser(input);
                parser.parse();
                document = parser.getPDDocument();
                // 获取内容信息
                PDFTextStripper pts = new PDFTextStripper();
                pts.setSortByPosition(sort);
                endPage = document.getNumberOfPages();
                log.debug("Total Page: " + endPage);
                pts.setStartPage(startPage);
                pts.setEndPage(endPage);
                try {
                    content = pts.getText(document);
                } catch (Exception e) {
                    throw e;
                }
                log.debug("Get PDF Content ...");
            } catch (Exception e) {
                throw e;
            } finally {
                if (null != document)
                    document.close();
            }
            content = content.replace("\n", "");
            content = content.replace("\\\\r", "");
            content = content.replace("\\\\t", "");
        } catch (Exception e) {
            log.error("getTextFromPdf [{}] is error", pdfPath);
        }
        return content;
    }

    /**
     * @Description:
     * @Param: fileUrlList：地址
     * @return: pages：页码数
     */
    public static int PDFToPage2(String fileUrlList) {
        //linux---start---
        fileUrlList = fileUrlList.replaceAll("\\\\", File.separator);
        //linux--end----
        File file = new File(fileUrlList);
        PdfReader pdfReader = null;
        try {
            pdfReader = new PdfReader(new FileInputStream(file));
        } catch (IOException e) {
            e.printStackTrace();
        }
        int pages = pdfReader.getNumberOfPages();
        log.debug("pdf文件的总页数为:" + pages);
        return pages;
    }
}
